package com.kehutong.applet.service.impl;

import com.kehutong.applet.entity.AdvertEntity;
import com.kehutong.applet.entity.AdvertPositionEntity;
import com.kehutong.applet.service.AdvertService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: kht
 * @description: 广告服务
 * @author: joneron
 * @create: 2021/5/21
 **/
public class AdvertServiceImpl implements AdvertService {

    private static final Logger logger = LoggerFactory.getLogger(AdvertServiceImpl.class);

    @Inject
    private JdbcSession jdbcSession;

    @Override
    public Page<AdvertEntity> findPage(JSONObject jsonObject) {

        FindPage<AdvertEntity> find = jdbcSession.findPage(AdvertEntity.class)
                .eq("deleted", false)
                .eq(jsonObject,"status")
                .eq(jsonObject,"positionId")
                .like(jsonObject,"title")
                .like(jsonObject,"endTime")
                .order("createTime", "desc");

        find.page(jsonObject);

        return find.exe();
    }

    @Override
    public List<AdvertEntity> findList(JSONObject jsonObject) {

        return jdbcSession.findArray(AdvertEntity.class)
                .eq("deleted", false)
                .eq("status",true)
                .eq(jsonObject,"positionId")
                .maxEq("endTime",jsonObject.getString("endTime"))
                .order("sortNum","desc")
                .exe();
    }

    @Override
    public Integer save(AdvertEntity advertEntity) {

        AdvertPositionEntity position = jdbcSession.findById(AdvertPositionEntity.class,advertEntity.getPositionId());
        if(null != position){
            advertEntity.setPositionName(position.getPositionName());
        }

        position.setAdvertNum(position.getAdvertNum()+1);
        jdbcSession.updateById(position);

        return jdbcSession.insert(advertEntity);
    }

    @Override
    public Integer update(AdvertEntity advertEntity) {
        return jdbcSession.updateById(advertEntity);
    }

    @Override
    public Integer deleteById(String id) {
        AdvertEntity result = Pool.get(AdvertEntity.class, id);
        if (result != null) {
            result.setDeleted(true);
            jdbcSession.updateById(result);
        }
        return 1;
    }

    @Override
    public int saveClickNum(String id) {

        AdvertEntity advertEntity = jdbcSession.findById(AdvertEntity.class,id);
        if(null != advertEntity){
            advertEntity.setClickNum(advertEntity.getClickNum() + 1);
            return update(advertEntity);
        }

        return 0;
    }
}
